"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : DN主+DN备故障	mv gaussdb，kill -9杀掉圈内dn主备进程
Create At   : 2020/06/29
Owner       : n@ningyali
Description :
    1、 主机执行，修改主机中配置文件中双机连接字符串
    2、 执行以下命令，启动数据库服务
    3、 恢复主机中配置文件
    4、 备机执行，修改备机中配置文件中双机连接字符串
    5、 执行以下命令，重启数据库服务
Expect      :
    1.修改成功
    2.stop成功，start失败，双机关系无法建立
    3.执行成功，重启成功
    4.修改成功
    5.stop成功，start失败，双机关系无法建立
History     :
    modified: 2020/12/09  modified by @wan005,恢复配置文件后 启动数据库概率性提示Degrade，在启动前增加等待，并修改断言,启动后增加状态校验。脚本重构。
"""

import os
import sys
import time
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *


class ConfParamError(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case089.py start------------')
        self.log.info('----------------------------初始化-----------------------------')
        self.conf_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.DB_PG_CONFIG_NAME)
        self.primaryNode = Node(node='PrimaryDbUser')
        self.standbyNode = Node(node='Standby1DbUser')
        self.com = Common()
        self.Constant = Constant()
        self.sh = CommonSH()

        # 备份配置文件
        bak_cmd = f"cp -R {self.conf_file_path} {self.conf_file_path}.baktest"
        self.log.info(bak_cmd)
        msg = self.primaryNode.sh(bak_cmd).result()
        self.log.info(msg)
        msg = self.standbyNode.sh(bak_cmd).result()
        self.log.info(msg)
        # 获取主备机hostname
        cmd = 'cat /etc/hostname'
        self.primary_name = self.primaryNode.sh(cmd).result()
        self.standby_name = self.standbyNode.sh(cmd).result()
        self.log.info('----------------------------主机获取配置参数replconninfo1-----------------------------')
        get_rep_cmd = f"cat {self.conf_file_path} | grep replconninfo1 "
        self.log.info(get_rep_cmd)
        rep_msg = self.primaryNode.sh(get_rep_cmd).result()
        self.log.info(rep_msg)
        self.assertIsNotNone(rep_msg)
        self.primary_ture_rep = rep_msg.split("'")[1].strip()

        self.log.info('----------------------------备机机获取配置参数replconninfo1-----------------------------')
        get_rep_cmd = f"cat {self.conf_file_path} | grep replconninfo1 "
        self.log.info(get_rep_cmd)
        rep_msg = self.standbyNode.sh(get_rep_cmd).result()
        self.log.info(rep_msg)
        self.assertIsNotNone(rep_msg)
        self.standby_ture_rep = rep_msg.split("'")[1].strip()


    # 主备配置文件的双机连接字符串配置错误
    def test_1_conf_param_error(self):
        self.log.info('----------------------------1、 主机执行，修改主机中配置文件中双机连接字符串-----------------------------')
        primary_wrong_rep = self.com.trans_num_to_alp(self.primary_ture_rep)
        status = self.sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 = '{primary_wrong_rep}'", self.primary_name)
        self.assertTrue(status)

        self.log.info('----------------------------2、 执行以下命令，启动数据库服务-----------------------------')
        stop_msg = self.sh.stopDbCluster()
        self.assertTrue(stop_msg)
        start_msg = self.sh.startDbCluster()
        self.assertFalse(start_msg)

        self.log.info('------------------------3、 恢复主机中配置文件---------------------')
        status = self.sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 = '{self.primary_ture_rep}'",self.primary_name)
        self.assertTrue(status)
        stop_msg = self.sh.stopDbCluster()
        self.assertTrue(stop_msg)
        time.sleep(10)
        start_msg = self.sh.startDbCluster(True)
        start_result = (self.Constant.START_SUCCESS_MSG in start_msg) or ('Degraded' in start_msg)
        self.assertTrue(start_result)
        time.sleep(25)
        self.log.info('查看数据库状态')
        status = self.sh.getDbClusterStatus("status")
        self.log.info(status)
        self.assertTrue(status)

        self.log.info('-----------------------4、 备机执行，修改备机中配置文件中双机连接字符串--------------------')
        standby_wrong_rep = self.com.trans_num_to_alp(self.standby_ture_rep)
        status = self.sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 = '{standby_wrong_rep}'", self.standby_name)
        self.assertTrue(status)

        self.log.info('----------------------------5、 执行以下命令，启动数据库服务-----------------------------')
        stop_msg = self.sh.stopDbCluster()
        self.assertTrue(stop_msg)
        start_msg = self.sh.startDbCluster()
        self.assertFalse(start_msg)

    def tearDown(self):
        self.log.info('----------------------------------this is tearDown----------------------------------')
        self.log.info('------------------------ 恢复主机中配置文件---------------------')
        status = self.sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 = '{self.primary_ture_rep}'", self.primary_name)
        self.assertTrue(status)

        self.log.info('------------------------ 恢复备机中配置文件---------------------')
        status = self.sh.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, f"replconninfo1 = '{self.standby_ture_rep}'", self.standby_name)
        self.assertTrue(status)

        stop_msg = self.sh.stopDbCluster()
        self.log.info(stop_msg)
        time.sleep(10)
        start_msg = self.sh.startDbCluster(True)
        self.log.info(start_msg)
        time.sleep(25)
        status = self.sh.getDbClusterStatus("status")
        self.log.info(status)